package com.vanke.vms.management.action;

import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionContext;
import com.vanke.vms.common.action.BaseAction;
import com.vanke.vms.common.domain.User;
import com.vanke.vms.management.service.LoginService;

public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 5320050254360655555L;

	private String username;

	private String password;

	private int result;

	private int validate;

	private int role;
	
	private LoginService loginService;

	private User user;

	public String logOut(){
		session.clear();
		return SUCCESS;
	}
	
	public String login() {
		result = 0;
		user = loginService.getUser(username);
		if (user == null){
			result = 1;
			return "none";
		}
		validate = user.getValidate();
		if(validate == 4)
			return "waiting";
		else if (validate == 3)
			return "freeze";
		if (! user.getPassword().equals(password)) {
			validate += 1;
			user.setValidate(validate);
			loginService.setValidate(user);
			return "error";
		} else {
			if (validate != 0) {
				user.setValidate(0);
				loginService.setValidate(user);
			}
			this.putSession(user);
			if(user.getGrade() == 2)
				role = 2;
			else if (user.getGrade() == 1)
				role = 1;
			else if(user.getGrade() == 0)
				role = 0;
			return "success"; // success��ǵð�ʧ�ܴ�������
		}
	}

	public String findpass(){
		user = loginService.getUser(username);
		if (user == null){
			result = 1;
			return "none";
		}
		else{
			loginService.setForgetPass(username);
			result = 2;
			return SUCCESS;
		}
	}
	
	public String verrifyUser(){
		if(session.get("user") == null){
			return LOGIN;
		}
		else
			return SUCCESS;
	}
	
	public String showLogin() {
		return LOGIN;
	}

	private void putSession(User user) {
		session = ActionContext.getContext().getSession();
		session.put("user", user);
	}
	/////////////////////////////////////////////////
	
	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public int getValidate() {
		return validate;
	}

	public void setValidate(int validate) {
		this.validate = validate;
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public LoginService getLoginService() {
		return loginService;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

}
